package com.jp.refreshlocalcacheclient.net.netty.thread;

import com.jp.refreshlocalcacheclient.net.netty.message.RequestMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2020/5/14 20:23
 */
@Service
public class DealMessagePoolService {

    /**
     * 配置提供，没有提供就用默认值
     */
    @Value("${refres.local.cache.client.deal.corePoolSize:2}")
    private int corePoolSize;
    /**
     * 配置提供，没有提供就用默认值
     */
    @Value("${refres.local.cache.client.deal.maximumPoolSize:10}")
    private int maximumPoolSize;


    ThreadPoolExecutor poolExecuter = new ThreadPoolExecutor(corePoolSize,
            maximumPoolSize,
            0L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
            new CommonThreadFactory("DealMessagePool"),
            new ThreadPoolExecutor.DiscardPolicy());


    public void execut(RequestMessage rm){
        poolExecuter.execute(rm);
    }

    public void shutdown () {
        poolExecuter.shutdown();
    }

    public void getActiveCount () {
        poolExecuter.getActiveCount();
    }

    public boolean remove (Runnable task) {
        return poolExecuter.remove(task);
    }

}
